//=====================================================================================================
//			Programm for the mavdek IDU 4-6 HP
//			
//			
//			
//			
//			
//=====================================================================================================

#define M_A_I_N
#include "global.h"
#include "version.h"


const char ITUHeader[4]={0xfe,0x02,0x03,0x6c};
const char ITUPreset[6]={0,0,0x64,0x00,0x3c,0x09};


const char IDUHeader[4]={0xfe,0x02,0x01,0x1a};
const char IDUPreset[20]={0x08,0x00,0x00,0x0b,0x00,0x02,0x49,0x1c,0x00,0x14,0x19,0x1a,0x16,0x00,0x18,0x19,0xec,0x98,0x00,0x00};
//const char IDUPreset[20]={0x00,0x00,0x00,0x00,0x03,0x06,22,16,0x00,0x00,0x00,24,24,0x00,24,0x00,24,0x00,0x00,0x00};
bank1 unsigned char ITUMsgId;
bank1 unsigned char IDUMsgId;

const char* const model_name[]={
"XXX",				// 0
"Hitachi 4Hp ",		// 1
"Hitachi 5Hp ",		// 2
"Toshiba 6Hp ",		// 3
"Electra 4Hp ",		// 4
"Toshiba 4Hp ",		// 5
"Toshiba 5Hp ",		// 6
"Reserved    "		// 7
 };

//=========================================================================================================
//
//			global variables initialization
//
//=========================================================================================================


bank1 union ComHeaderType MyODUIDUHeader;
bank1 union ComHeaderType MyIDUHeader;
bank1 union ComHeaderType MyODUkCOMHeader;
bank2 union ODUType MyODU;
bank2 union IDUType MyIDU;
bank2 union ITUType MyITU;
bank2 union ITUType MyITU_answer;
bank1 struct PresetType myPreset;
bank1 struct PresetType myAnswer;
unsigned char preset_index;


bank2 unsigned char counter1, button;

volatile en_group sourse,destination;

bit flag_pulse_tick;
bit flag_system_tick;
bit	flag_packet_received;	 	
bit	flag_packet_good;
bit	flag_packet_sent;
bit	flag_com_pause;
bit	flag_rx_error;
bit flag_itu_start;
bank3 int test_duration,rest_of_test,rest_of_stage;
//=========================================================================================================
//
//			Function section
//
//=========================================================================================================


void PresetToITU (void)
{
	MyITU.Data.OOprEEV  = myPreset.EEV_value;
	MyITU.Data.CompFreq = myPreset.Compressor_freq;
	MyITU.Data.OOprOFan = myPreset.Fan_speed;
	MyITU.Data.IOprMMode = myPreset.Mode;
};


//=============================================================================================================
// PROTOTYPES
//=============================================================================================================
 void LCDContrastON(void);
 
//
//=============================================================================================================

//=============================================================================================================
//				M		M    AAA     II  N     N
//				M M   M	M	A	A	 II  N N   N
//				M  M M	M  A     A   II  N  N  N
//				M   M   M  AAAAAAA   II  N   N N
//				M       M  A     A   II  N    NN
//				M       M  A     A   II  N     N
//=============================================================================================================
void main (void)
{ 
	unsigned char i;
	unsigned char m_verdict;
	static bank3 unsigned int main_timeout_counter=0;
		PIE1=PIE2=INTCON=0;


		InitLCD();
		ClearLCD();
		init_comms();
		TXIE=0;
		TXEN=1;
		RCIE=1;
		InitTMR1();
		GIE=1;
		resetTime();
 		LCDContrastON();
//========================================

		flag_sequence_running =1;

//========================================



for ( i=0; i<6;i++)	
{	MyITU.access[i]=ITUPreset[i];
}


for ( i=0; i<20;i++)	
{	MyIDU.access[i]=IDUPreset[i];
}

//===================================================================================================
//				E N D L E S S    L O O P
//===================================================================================================
ClearLCD();
CursorPosLCD(1,0);
printf (___VERSION);
CursorPosLCD(2,0);
printf ("   Waiting  for     ");
CursorPosLCD(3,0);
printf ("   communication    ");


while ( !flag_packet_good )
{	
	if (flag_system_tick)
	{	flag_system_tick=0;


		CursorPosLCD(4,8);
		printf ("%d   ",100-main_timeout_counter);

		if (main_timeout_counter++ >100) 
		{			
			ClearLCD();
			CursorPosLCD(1,7);
			printf ("ERROR!");
			CursorPosLCD(2,2);
			printf ("NO COMMUNICATION");
			CursorPosLCD(3,4);
			printf ("TEST STOPPED");
			while(1);		
		};
	};
};
flag_packet_good=0;

ClearLCD();
CursorPosLCD(2,0);
printf ("Driver: ");
	job_selector = 0;
if (MyODU.Data.ODUExtraData.ODUModel4==0)
{
	if (13 == MyODU.Data.ODUExtraData.ODUModel)
	{
		job_selector=1;
		test_duration = GetSequenceDuration(DCI_HITACHI_4HP);
		MyIDU.Data.IDUBasicData.IXInfModel = 0; // modela A
		MyIDU.Data.IDUExtraData.IXInfFamily = 2;  // 
		MyIDU.Data.IDUExtraData.IXInfFamily_1 = 2; //  family 18
	};
	if (14 == MyODU.Data.ODUExtraData.ODUModel)
	{ 
		job_selector=2;
		test_duration = GetSequenceDuration(DCI_HITACHI_5HP);
		MyIDU.Data.IDUBasicData.IXInfModel = 1;  //model B
		MyIDU.Data.IDUExtraData.IXInfFamily = 2;
		MyIDU.Data.IDUExtraData.IXInfFamily_1 = 2;
	};
	if (15 == MyODU.Data.ODUExtraData.ODUModel)
	{ 
		job_selector=3;
		test_duration = GetSequenceDuration(DCI_TOSHIBA_6HP);
		MyIDU.Data.IDUBasicData.IXInfModel = 2; // MODEL C  TBD  
		MyIDU.Data.IDUExtraData.IXInfFamily = 2;
		MyIDU.Data.IDUExtraData.IXInfFamily_1 = 2;
	};
};
if (MyODU.Data.ODUExtraData.ODUModel4==1)
{
	if (12 == MyODU.Data.ODUExtraData.ODUModel)
	{
		job_selector=4;
		test_duration = GetSequenceDuration(DCI_ELECTRA_4HP);
		MyIDU.Data.IDUBasicData.IXInfModel = 0; // lets say it A model  see 362v0 - d spec matching table
		MyIDU.Data.IDUExtraData.IXInfFamily = 3;
		MyIDU.Data.IDUExtraData.IXInfFamily_1 = 2;

	};
	
	
	if (13 == MyODU.Data.ODUExtraData.ODUModel)
	{ 
		job_selector=5;
		test_duration = GetSequenceDuration(DCI_TOSHIBA_4HP);
	};
	if (14 == MyODU.Data.ODUExtraData.ODUModel)
	{ 
		job_selector=6;
		test_duration = GetSequenceDuration(DCI_TOSHIBA_5HP);
	};
}


if (!job_selector)
{
	printf ("%d",MyODU.Data.ODUExtraData.ODUModel);
	CursorPosLCD(3,0);
	printf("  Dipswitch  ERROR! ");
	CursorPosLCD(1,0);
	printf("___ TEST  FAILED ___");
	CursorPosLCD(4,0);
	printf("   Disconnect Unit  ");
	while(1)continue;
}

printf ("%s",model_name[job_selector]);


main_timeout_counter=0;
while( main_timeout_counter <10 )
{
	if (flag_system_tick)
	{	flag_system_tick=0;
		main_timeout_counter++;
	};
};
ClearLCD();


if (MyODU.Data.ODUBasicData.OOprInFlt)
{
	if ((MyODU.Data.ODUBasicData.OOprLstFlr&0b00011111)!=16)
	{	CursorPosLCD(0,7); 
		printf ("ERROR!");
		CursorPosLCD(1,0);
		printf ("    ODU FAILURE     "); 
		CursorPosLCD(2,0); 
		printf("%s", Error_explanation[MyODU.Data.ODUBasicData.OOprLstFlr&0b00011111]);
		CursorPosLCD(3,4);
		printf ("TEST STOPPED");
		while(1);
	}			
};

// ================================================================================================
// main loop 
//=================================================================================================



resetTime();
while (1)
	{	
		flag_itu_start=1;
		if (flag_system_tick)
		{	flag_system_tick=0;
			if (flag_packet_good)
			{	flag_packet_good=0;
				PresetToITU ();
			}; // END of packet good
		}; //end of system tick

		if (flag_pulse_tick)
		{	flag_pulse_tick=0;

			if (!flag_sequence_finished)
			{	DoTest();
				CheckForRuntimeError();
			}
			else
			{
				DisplayResult();				
			};
		};
	}; // end of endless loop
};	// end of main


//=========================================================================================
//  need meandr 20khz 0.5 duty cycle
//	4Mhz   period register 40 for the 24kHz ( 0x28)
//
//
//
//
//
//=========================================================================================
void LCDContrastON(void)
{
	TMR2IE =0;
	CCP2IE=0;
	PR2=0x28;  //  set period 
	CCPR2L =0x14; //  set duty
	TRISC&=0b11111101; // 0xFB
	T2CON = 0x04; // 
	CCP2CON =0x0f;  // 
}; // 

//=========================================================================================
// EOF

